package com.yeshen.pmtea.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.yeshen.pmtea.beans.DBHelper;
import com.yeshen.pmtea.beans.News;

import java.util.ArrayList;
import java.util.List;



/**
 * Created by yuyanghui on 16/9/19.
 *  用与进行数据的的数据增删改查
 */
public class SqliteUtils {
    private static DBHelper dbHelper ;
    //往数据库添加数据
    public static void insertToSQL(Context context, String id, String title,String source, String description, String wap_content, String create_time){

        dbHelper = new DBHelper(context);

        SQLiteDatabase db = dbHelper.getReadableDatabase();

        ContentValues values = new ContentValues();

        values.put("id",id);
        values.put("title", title);
        values.put("source",source);
        values.put("description",description);
        values.put("wap_content",wap_content);
        values.put("create_time",create_time);
        db.insert("collect",null,values);
    }
    //在数据库中删除数据
    //查询数据库中的数据
    public static List<News> getListFromSQL(Context context){
        dbHelper = new DBHelper(context);
        News news;
        List<News> dataList = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from collect",null);
        while (cursor.moveToNext()) {
            news = new News();
            news.setId(cursor.getString(cursor.getColumnIndex("id")));
            news.setTitle(cursor.getString(cursor.getColumnIndex("title")));
            news.setSource(cursor.getString(cursor.getColumnIndex("source")));
            //news.setDescription(cursor.getString(cursor.getColumnIndex("description")));
            news.setWap_thumb(cursor.getString(cursor.getColumnIndex("description")));
            news.setCreate_time(cursor.getString(cursor.getColumnIndex("wap_content")));
            dataList.add(news);
        }
        return dataList;

    }



}
